package com.net.retrofits.manager;


import com.net.retrofits.common.RequestBack;

import java.lang.ref.SoftReference;




/**
 * Created by Administrator on 2016/10/20.
 */
public abstract class BaseManager {

    /**
     * 网络错误
     */
    public static final int WHAT_LOCALITY_NET_WORK_ERROR = 201;
    /**
     * 业务处理成功
     */
    public static final int WHAT_DEAL_SUCCEED = 300;
    /**
     * 业务操作失败
     */
    public static final int WHAT_DEAL_FAILED = 301;
    /**
     * 进度
     */
    public static final int WHAT_LOADING_PROGRESS = 299;
    //protected HandleCall handleCall = new HandleCall();
    //使用方法 BaseNetSource.setProgressListener(progress);


    SoftReference<RequestBack> requestBacks = null;

    public BaseManager(RequestBack requestBack) {
        if (requestBack == null) {
            return;
        }
        requestBacks = new SoftReference<RequestBack>(requestBack);
    }

    private RequestBack getRequestBack() {
        if (requestBacks == null) {
            return null;
        }
        RequestBack requestBack = requestBacks.get();
        return requestBack;
    }

   /*public Handler getHandleCall() {
        return handleCall;
    }*/

    public void onBack(int code, Object obj, String msg, String other, boolean isExchange) {
        /*if (isExchange) {
            Message message = handleCall.obtainMessage();
            message.obj = obj;
            message.what = -200;
            Bundle bundle = new Bundle();
            bundle.putInt("code", code);
            bundle.putString("msg", msg);
            bundle.putString("other", other);
            message.setData(bundle);
            handleCall.sendMessage(message);
            return;
        }*/
        RequestBack requestBack = getRequestBack();
        if (requestBack == null) {
            return;
        }
        requestBack.onBack(code, obj, msg, other);
    }

    //网络请求的handle
 /*   protected void netHandleMessage(Message msg) {
        //-200 :请求 -201：进度
        int what = msg.what;
        RequestBack requestBack = getRequestBack();
        if (requestBack == null) {
            return;
        }
        switch (what) {
            case -200:
                //api请求成功
                Object obj = msg.obj;
                Bundle bundle = msg.getData();
                int code = bundle.getInt("code", -1);
                String hint = bundle.getString("msg");
                String other = bundle.getString("other");
                requestBack.onBack(code, obj, hint, other);
                break;
            case -201:
                //进度
                bundle = msg.getData();
                int whatCode = bundle.getInt("what", -1);
                String url = bundle.getString("url");
                String filePath = bundle.getString("filePath");
                long progress = bundle.getLong("progress", 0);
                long total = bundle.getLong("total", 0);
                boolean isOnBack = bundle.getBoolean("isOnBack", false);
                if (!isOnBack) {
                    requestBack.onBackProgress(whatCode, url, filePath, progress, total);
                } else {
                    requestBack.onBack(WHAT_LOADING_PROGRESS, whatCode == 3, String.valueOf(progress), String.valueOf(total));
                }
                break;
        }


    } */


   /* class HandleCall extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            netHandleMessage(msg);
        }
    }*/

}
